package com.dopies.bsdmall.activity;

import java.util.Calendar;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.dopies.BeeFramework.activity.BaseActivity;
import com.dopies.BeeFramework.model.BusinessResponse;
import com.dopies.BeeFramework.view.ToastView;
import com.dopies.bsdmall.R;
import com.dopies.bsdmall.model.AccountModel;
import com.dopies.bsdmall.utils.NumberUtils;
import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListView.IXListViewListener;

/**
 * 用户积分提现
 */
public class CheckCashActivity extends BaseActivity implements
		BusinessResponse, IXListViewListener {
	private ImageView back;
	private EditText score; // 积分
	private EditText desc; // 描述
	private ProgressDialog pd = null;
	private FrameLayout check;
	private AccountModel accountModel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.check_cash);
		back = (ImageView) findViewById(R.id.address_manage_back);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		score = (EditText) findViewById(R.id.check_score);
		desc = (EditText) findViewById(R.id.check_desc);
		check = (FrameLayout) findViewById(R.id.check_button);

		check.setOnClickListener(new OnClickListener() {

			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				String input_score = score.getText().toString();
				String input_desc = desc.getText().toString();
				Calendar c = Calendar.getInstance();
				String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
				if (!"6".equals(mWay)) {
					ToastView toast = new ToastView(CheckCashActivity.this,
							"未到时间不能提现，限周五可以提现！");
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else if (null == input_score || input_score.isEmpty()) {
					ToastView toast = new ToastView(CheckCashActivity.this,
							"积分值不能为空");
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else if (null == input_desc || input_desc.isEmpty()) {
					ToastView toast = new ToastView(CheckCashActivity.this,
							"户名; 卡号; 开户行不能为空");
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else if (!NumberUtils.isNumber(input_score)) {
					ToastView toast = new ToastView(CheckCashActivity.this,
							"积分必须是数字");
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else {
					accountModel = new AccountModel(CheckCashActivity.this);
					accountModel.addResponseListener(CheckCashActivity.this);
					accountModel.checkAccount(input_score, input_desc);
				}
			}
		});
	}

	@Override
	public void onRefresh(int id) {

	}

	@Override
	public void onLoadMore(int id) {

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url == null)
			return;
		String noc = "";
		noc = accountModel.msg;
		Intent intent = new Intent();
		intent.putExtra("login", true);
		setResult(Activity.RESULT_OK, intent);
		finish();
		overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
		ToastView toast = new ToastView(CheckCashActivity.this, noc);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

}
